package cn.lg.org.controller;


import cn.lg.basic.util.AjaxResult;
import cn.lg.basic.util.PageList;
import cn.lg.org.domain.Employee;
import cn.lg.org.query.EmployeeQuery;
import cn.lg.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门管理接口
 *
 */
@RestController
@RequestMapping("/emp")
//接口文档的注解
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    /**
     * 查询所有
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询所有" )
    public List<Employee> queryAll(){
        return employeeService.queryAll();
    }

    /**
     * 查询单个
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Employee queryById(@ApiParam(name="编号",required = true) @PathVariable("id") Long id){
        return employeeService.queryById(id);
    }


    /**
     * 分页查询
     * @param employeeQuery
     * @return
     */
    @PostMapping
    @ApiOperation(value = "分页查询" )
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery employeeQuery){
        return employeeService.queryData(employeeQuery);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过ID删除" )
    public AjaxResult remove(@ApiParam(name="编号",required = true) @PathVariable("id")Long id) {
        try {
            employeeService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("系统繁忙，请稍后重试!!!");
        }
    }

    /**
     * 添加或修改操作
     * @param employee
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加或修改" )
    public AjaxResult addOrUpdate(@RequestBody Employee employee) {
        try {
            if (employee.getId() != null){
                employeeService.update(employee);
            }else{
                employeeService.save(employee);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("系统繁忙，请稍后重试!!!");
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public AjaxResult batchRomove(@RequestBody List<Long> ids) {
        try {
            employeeService.patchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("系统繁忙，请稍后重试!!!");
        }
    }
}
